﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

public partial class showBook : System.Web.UI.Page
{
    private static string[] str = null;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   

            BookInfo books = new BookInfo();
            if (Request.QueryString["ID"] != null)
            {
                currentpage.Text = "1";  //首次加载，把目前页置1
                string message = Request.QueryString["ID"]; //解析请求页面传来的字符串参数
                str = message.Split('|');
                judge(str, books);  //调用判断函数
            }
        }
    }


    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        
        //向目标页面传递数据
            if (e.CommandName.ToLower() == "see")
            {
                string sUrl = "~/Customer/inquiry.aspx?id=" + e.CommandArgument;
                int id = Convert.ToInt32(e.CommandArgument);
                BookInfo book = new BookInfo();
                book.increaseHitNum(id);
                Response.Redirect(sUrl);
            }
    
    }

    //用于每一页显示时重新绑定数据源
    private void repdatabind(IEnumerable<BookInfo> bs)
    {
        PagedDataSource pds = new PagedDataSource();
        pds.DataSource = bs;                       //定义分页控件的数据源
        pds.AllowPaging = true;
        pds.PageSize = 4;
        pds.CurrentPageIndex = Convert.ToInt32(currentpage.Text) - 1;
        this.BtnDown.Enabled = true;
        this.BtnUp.Enabled = true;

        if (pds.IsFirstPage)
        {
            this.BtnUp.Enabled = false;
        }
        if (pds.IsLastPage)
        {
            this.BtnDown.Enabled = false;
        }
        this.DataList1.DataSource = pds;        //绑定数据显示控件到分页控件
        this.DataList1.DataBind();
    }

    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    //向上翻页按钮
    protected void BtnUp_Click(object sender, EventArgs e)
    {
        this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) - 1);
        BookInfo books = new BookInfo();
        judge(str, books); 

    }
    //向下翻页按钮
    protected void BtnDown_Click(object sender, EventArgs e)
    {
        this.currentpage.Text = Convert.ToString(Convert.ToInt32(currentpage.Text) + 1);
        BookInfo books = new BookInfo();
        judge(str, books); 

    }
    //判断函数，根据传入的参数字符串的值选择数据源
    void judge(string []strs,BookInfo books)
    {
         if (strs[0] == "superType")
        {
            int id = Convert.ToInt32(str[1]);
            repdatabind(books.GetBookBySuperType(id));

        }
        if (strs[0] == "subType")
        {
            int id = Convert.ToInt32(str[1]);
            repdatabind(books.GetBookBySubType(id));


        }
        if (strs[0] == "inquiryBtn1")
        {
            repdatabind(books.GetBookByKeyWord(str[1]));


        }
        if (strs[0] == "inquiryBtn2")
        {
            repdatabind(books.GetBookByAuthor(str[1]));
        }
    }

}